<?php
/**
 * JsClasses Multiple Backends Edition
 * BackEnd Class
 *
 * @copyright  2010 RIA Allegro
 * @license    GNU LGPL
 * @version    1.3.1 (2010-05-24)
 * @author     Vasiliy.Lyalyuk@gmail.com
 * @link       http://linux.ria.ua/JsClasses/
 */
class JsClassesBackend  extends JsClassesMemcache{
    /**
     * Server path Frontend where real JavaScript file
     *
     * @var string
     */    
   private $_frontendDirUrl='http://localhost/';

    /**
     * frontend Script Name
     *
     * @var string
     */    
   private $_frontendScriptName='indexFrontend.php';   
   
   /**
     * Server error if not file
     *       
     * @var string
     */
   private $_errorServerIfNotFile='404 Not Found';





    /**
     * Checks of all classes and feature updates to jsClasses.cache,
     * and sends the request if necessary
     * to regenerate jsClasses.js on a remote server
     *
     * @param array $classes array of JSClasses without '.js' extension.
     * @example
     *   new JsClasses(array('Ria_JsonRequest','RIA_HtmlRequest'));
     */

    public function make($classes) {
        $this->_loadClassesCache();
        $this->_parseClasses($classes);
        $this->_checkClasses();

        $file_headers = @get_headers($this->_frontendDirUrl.$this->_getJsRealFileName());
        if($file_headers[0] == $this->_ErrorServerIfNotFile) {
           $this->_isCacheRebuildRequired = true;
        }

        if ($this->_isCacheRebuildRequired) {
            $JsClassesText=implode(',',$classes);
            $NewName=fopen($this->_frontendDirUrl.$this->_frontendScriptName.'?'.$this->_getParamName."=$JsClassesText", 'r');
            if (!$NewName) {
                $this->trigger_error('Not open file generate script on remove server!',E_USER_ERROR);
            }
            $this->_jsFileName=stream_get_contents($NewName);
            fclose($NewName);
        }
    }

 
    /**
     * Set text error if not file on server
     *
     * @param string 
     */
    public function setErrorServerIfNotFile($errorServerIfNotFile) {
    	$this->_errorServerIfNotFile =$errorServerIfNotFile;
    }   
    
    /**
     * Set frontend Script Name
     *
     * @param string 
     */
    public function setFrontendScriptName($frontendScriptName) {
        $this->_frontendScriptName = $frontendScriptName;
    }    
    
    /**
     * Set Patch Server Name Frontend
     *
     * @param string 
     */
    public function setFrontendDirUrl($frontendDirUrl) {
        $this->_frontendDirUrl = $frontendDirUrl;
    }

    /**
     * Get path Virtual Cache JavaScript file
     * from remote root web-server directory
     *
     * @return string
     */
    public function getJsFileName() {
        return $this->_frontendDirUrl.$this->_jsFilesDir . '/' .$this->_jsFileName;
    }



}
?>